<?php

class importexport_ctl_admin_export extends importexport_controller {

    public function queue_export() {
        $params = array(
            'title' => '导出任务队列',
            'use_buildin_recycle' => true,
            'orderBy' => 'create_date desc',
            'base_filter' => array(
                'type' => 'export'
            )
        );
        $this->finder('importexport_mdl_task', $params);
    }

    public function export_view() {
        
        // 导出方式
        $this->pagedata['check_policy'] = $this->check_policy();
        
        // 导出
        $this->pagedata['params'] = serialize($_GET['_params']);
        $this->pagedata['filter'] = serialize($_POST);
        
        // 支持导出类型
        $this->pagedata['export_type'] = $this->export_support_filetype();
        $this->display('admin/export/export.html');
    }

    public function create_export() {
        $filter = unserialize($_POST['filter']);
        $params = unserialize($_POST['params']);
        if (isset($filter['isSelectedAll']) && $filter['isSelectedAll'] == '_ALL_') {
            if (isset($filter['filter'])) {
                foreach ($filter['filter'] as $key => $value) {
                    $filter[$key] = $value;
                }
            }
            $filter = $this->view_filter($filter, $params);
        }
        
        $_POST['key'] = $this->gen_key();
        $queue_params = array(
            'filter' => $filter,
            'app_id' => $params['app'],
            'model' => $params['mdl'],
            'filetype' => $_POST['filetype'],
            'policy' => $this->queue_policy(),
            'key' => $_POST['key'],
            'suffix' => $params['suffix']
        );
        // system_queue::instance()->publish('importexport_tasks_runexport', 'importexport_tasks_runexport', $queue_params);
        app::get('importexport')->model('task')->create_task('export', $_POST);
        // 测试，直接导出数据到存储服务器，不进行队列
        // print_r($queue_params);exit;
        kernel::single('importexport_tasks_runexport')->exec($queue_params);
    }

    public function queue_download() {
        if (! $this->file_download($msg)) {
            echo $msg;
            exit();
        }
    }
}
